package plantshop.model;

import java.io.Serializable;


public class CollisionDetector implements Serializable{
	private static final long serialVersionUID = -8222145988422563924L;
	private static Store store;
	private static double xscale, yscale;
	
	public static void setXScale(double s){
		xscale = s;
	}
	public static void setYScale(double s){
		yscale = s;
	}
	
	public static void setStore(Store s)
	{
		store = s;
	}
	
	public static int isValid(Location loc, Customer customer)
	{
		if(loc.getX() < store.getTopLeft().getX() || loc.getX() > store.getBottomRight().getX()-Customer.CUST_WIDTH)
			return -1;
		if(loc.getY() < store.getTopLeft().getY() || loc.getY() > store.getBottomRight().getY()-Customer.CUST_HEIGHT)
			return -1;
		
		if(loc.getX() > store.getBottomRight().getX()-Customer.CUST_WIDTH-100*xscale && loc.getY() > store.getBottomRight().getY()-Customer.CUST_HEIGHT-320*yscale)
			return -1;
		if(loc.getX() > store.getBottomRight().getX()-Customer.CUST_WIDTH-100*xscale && loc.getY() < store.getTopLeft().getY()-270*yscale)
			return -1;
		
		for(Display d : store.getFront())
		{
			if(loc.getX() <= d.getBottomRight().getX() && loc.getX() >= d.getTopLeft().getX()) //Top left corner
				if(loc.getY() <= d.getBottomRight().getY() && loc.getY() >= d.getTopLeft().getY())
					return -1;
			if(loc.getX() + Customer.CUST_WIDTH <= d.getBottomRight().getX() && loc.getX() + Customer.CUST_WIDTH >= d.getTopLeft().getX()) //Bottom right corner
				if(loc.getY() + Customer.CUST_HEIGHT <= d.getBottomRight().getY() && loc.getY() + Customer.CUST_HEIGHT >= d.getTopLeft().getY())
					return -1;
			if(loc.getX() + Customer.CUST_WIDTH <= d.getBottomRight().getX() && loc.getX() + Customer.CUST_WIDTH >= d.getTopLeft().getX()) //Top right corner
				if(loc.getY() <= d.getBottomRight().getY() && loc.getY() >= d.getTopLeft().getY())
					return -1;
			if(loc.getX() <= d.getBottomRight().getX() && loc.getX() >= d.getTopLeft().getX()) //Bottom left corner
				if(loc.getY() + Customer.CUST_HEIGHT <= d.getBottomRight().getY() && loc.getY() + Customer.CUST_HEIGHT >= d.getTopLeft().getY())
					return -1;
		}
		
		for(Customer c : store.getCustomers())
		{
			if(c != customer)
			{
				if(loc.getX() <= c.getLoc().getX() + Customer.CUST_WIDTH && loc.getX() >= c.getLoc().getX()) //Top left corner
					if(loc.getY() <= c.getLoc().getY() + Customer.CUST_HEIGHT && loc.getY() >= c.getLoc().getY())
						return 0;
				if(loc.getX() + Customer.CUST_WIDTH <= c.getLoc().getX() + Customer.CUST_WIDTH && loc.getX() + Customer.CUST_WIDTH >= c.getLoc().getX()) //Bottom right corner
					if(loc.getY() + Customer.CUST_HEIGHT <= c.getLoc().getY() + Customer.CUST_HEIGHT && loc.getY() + Customer.CUST_HEIGHT >= c.getLoc().getY())
						return 0;
				if(loc.getX() + Customer.CUST_WIDTH <= c.getLoc().getX() + Customer.CUST_WIDTH && loc.getX() + Customer.CUST_WIDTH >= c.getLoc().getX()) //Top right corner
					if(loc.getY() <= c.getLoc().getY() + Customer.CUST_HEIGHT && loc.getY() >= c.getLoc().getY())
						return 0;
				if(loc.getX() <= c.getLoc().getX() + Customer.CUST_WIDTH && loc.getX() >= c.getLoc().getX()) //Bottom left corner
					if(loc.getY() + Customer.CUST_HEIGHT <= c.getLoc().getY() + Customer.CUST_HEIGHT && loc.getY() + Customer.CUST_HEIGHT >= c.getLoc().getY())
						return 0;
			}
		}
		
		return 1;
	}
}
